// Copyright (c) 2010, Anthony Cassidy
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are 
// permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice, this list 
//         of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice, this 
//         list of conditions and the following disclaimer in the documentation and/or 
//         other materials provided with the distribution.
//     * Neither the name of the AntsAdventureGameCreator nor the names of its contributors 
//         may be used to endorse or promote products derived from this software without 
//         specific prior written permission.
// 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
// THE POSSIBILITY OF SUCH DAMAGE.
#include "XStdafx.h"
#include "KeywordRegistry.h"
#ifdef _DEBUG
#ifdef WIN32
    #include "atltrace.h"
#endif
#endif
/*! @cond */



bool KeywordRegistry::RegisterKeyword(const char* keyword, int mappingNumber)
{
    if(!keyword)
    {
        ASSERT(false && "RegisterKeyword(keyword,int) trying to be called with NULL POINTER!");
        return false;
    }

    if(*keyword == 0)
    {
        ASSERT(false && "RegisterKeyword(keyword,int) trying to be called with EMPTY STRING!");
        return false;
    }

    if(m_currentMapWaitingForACppName.find(keyword) != m_currentMapWaitingForACppName.end())
    {
        //ASSERT(false && "RegisterKeyword(keyword,int) trying to register a word when it ALREADY EXISTS!");
        return false;
    }

    m_currentMapWaitingForACppName[std::string(keyword)] = mappingNumber;

    return true;
}

int KeywordRegistry::GetCodeFor(const char* room, const char* string)
{
    if(!room || !string)
    {
        ASSERT(false && "GetCodeFor(string) trying to be called with NULL POINTER!");
        return -1;
    }
    if(room == 0)
    {
        ASSERT(false && "GetCodeFor(string) trying to be called with Room as Empty String!");
        return -1;
    }
    if(string == 0)
    {
        return 0; // this is the only time we return 0;
    }
    if(m_mapOfMaps.find(room) == m_mapOfMaps.end())
    {
        ASSERT(false && "At(string) trying to access Unregistered Room!");
        return -1;
    }
    QMap<std::string, int>& keywordMap = m_mapOfMaps[room];
    if(keywordMap.find(std::string(string)) == keywordMap.end())
    {
        //ASSERT(false && "At(string) trying to access Unregistered Keyword!");
        // since we execute every command line in many differnt rooms,
        // there will always be rooms called with keywords
        // that they know nothing about.
        return -1;
    }

    int toReturn = keywordMap[string];
    return toReturn;
}

int KeywordRegistry::SetCurrentRoomKeyword(const char* roomKeyword)
{
    // do this here, so we can easily find problem in debug output
#ifdef _DEBUG
#ifdef WIN32
    QMap<std::string, int>::iterator iter = m_currentMapWaitingForACppName.begin();
    for(;iter != m_currentMapWaitingForACppName.end();iter++)
    {
        ATLTRACE("keyword: %s %s\n", roomKeyword, iter.key().c_str());
    }
#endif
#endif



    m_mapOfMaps[roomKeyword] = m_currentMapWaitingForACppName;
    m_currentMapWaitingForACppName.clear();

    int thisValueIsNeededByDynamicInitializationTrick = 0;
    return thisValueIsNeededByDynamicInitializationTrick;
}

/*! @endcond */
